package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocDispatchAddDTO;
import org.jsola.hr.dto.SocDispatchUpdateDTO;
import org.jsola.hr.query.SocDispatchQuery;
import org.jsola.hr.service.ISocDispatchService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 社保派单记录表
 *
 * @author zhr
 */
@RestController("hrSocDispatchController")
@RequestMapping(HrConstants.API_PATH + "/v1/socDispatches")
public class SocDispatchController implements ISocDispatchControllerAPI {

    @Autowired
    private ISocDispatchService socDispatchService;






    @Override
    @DeleteMapping(value = "/{socDispatchId}")
    public Result<Integer> delete(@PathVariable String socDispatchId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保派单记录表权限校验

        int count = socDispatchService.deleteByIds(tokenUser, socDispatchId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socDispatchIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保派单记录表权限校验

        int count = socDispatchService.deleteByIds(tokenUser, socDispatchIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socDispatchId}")
    public Result<SocDispatchVO> findById(@PathVariable String socDispatchId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单记录表权限校验

        return Result.success(
            socDispatchService.selectById(socDispatchId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocDispatchListVO>> listAll(@Valid SocDispatchQuery socDispatchQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单记录表权限校验

        return Result.success(
            socDispatchService.select(socDispatchQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocDispatchListVO>> page(@Valid SocDispatchQuery socDispatchQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socDispatchService.selectPage(socDispatchQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getLastMonthInfo/{companyId}/{year}/{month}")
    public Result<SocDispatchLastMonthVO> getLastMonthInfo(@PathVariable String companyId,
                                                           @PathVariable String year,
                                                           @PathVariable String month) {

        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                socDispatchService.getLastMonthInfo(companyId, year, month, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/findDetails/{socDispatchId}")
    public Result<SocDetailsDispatchVO> findDetailsById(@PathVariable String socDispatchId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单记录表权限校验

        return Result.success(
                socDispatchService.findDetailsById(socDispatchId, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "/dispatchSend/{socDispatchId}")
    public Result<Integer> dispatchSend(@PathVariable String socDispatchId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                socDispatchService.dispatchSend(socDispatchId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listChildCompanyBySoc/{socId}/{companyId}")
    public Result<List<ChildCompanyListVO>> listChildCompanyBySoc(@PathVariable String socId,
                                                                  @PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                socDispatchService.listChildCompanyBySoc(socId, companyId, tokenUser.getSiteId())
        );
    }


}

